#include "RenderTextureManager.hpp"

namespace sl
{
	namespace tmp
	{
		sf::RenderTexture* RenderTextureManagerS::Get(const std::string& name)
		{
			if (myIsActive)
				return ManagerPointer<sf::RenderTexture>::Get(name);
			else
				return NULL;
		}

		sf::RenderTexture* RenderTextureManagerS::Create(const std::string& name, unsigned int width, unsigned int height, bool depthBuffer)
		{
			sf::RenderTexture* img = new sf::RenderTexture;
			img->Create(width, height, depthBuffer);

			return Insert(name, img);
		}

		RenderTextureManagerS* RenderTextureManagerS::Clear(const std::string& name, const sf::Color& color)
		{
			sf::RenderTexture* img = Get(name);
			if (img == NULL)
				return NULL;

			img->Clear(color);

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::Clear(const sf::Color& color)
		{
			for (std::map<std::string, sf::RenderTexture*>::iterator iter = myData.begin(); iter != myData.end(); iter++)
				iter->second->Clear(color);

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::Display(const std::string& name)
		{
			sf::RenderTexture* img = Get(name);
			if (img == NULL)
				return NULL;

			img->Display();

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::Display()
		{
			for (std::map<std::string, sf::RenderTexture*>::iterator iter = myData.begin(); iter != myData.end(); iter++)
				iter->second->Display();

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::Draw(const std::string& name, sf::RenderTarget* tar, float x, float y)
		{
			sf::RenderTexture* img = Get(name);

			if (img == NULL)
				return NULL;

			if (tar == NULL)
				if (name == "screen")
					tar = (*RenderScreen)();
				else
				{
					tar = Get("screen");
					if (tar == NULL)
						return NULL;
					return Draw(name, tar, x, y);
				}

			sf::Sprite stmp;
			stmp.SetPosition(x, y);
			sf::Image itmp;
			sf::Texture ttmp;

			itmp = img->GetTexture().CopyToImage();
			itmp.CreateMaskFromColor(sf::Color::Magenta);
			ttmp.LoadFromImage(itmp);
			stmp.SetTexture(ttmp);

			tar->Draw(stmp);

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::Draw(sf::RenderTarget* tar, float x, float y)
		{
			if (tar == NULL)
			{
				tar = Get("screen");
				if (tar == NULL)
					return NULL;
				return Draw(tar, x, y);
			}

			sf::Sprite stmp;
			stmp.SetPosition(x, y);
			sf::Image itmp;
			sf::Texture ttmp;
			for (std::map<std::string, sf::RenderTexture*>::iterator iter = myData.begin(); iter != myData.end(); iter++)
			{
				itmp = iter->second->GetTexture().CopyToImage();
				itmp.CreateMaskFromColor(sf::Color::Magenta);
				ttmp.LoadFromImage(itmp);
				stmp.SetTexture(ttmp);

				if (iter->first != "screen")
					tar->Draw(stmp);
			}

			return this;
		}

		RenderTextureManagerS* RenderTextureManagerS::SetActive(bool active)
		{
			myIsActive = active;
			return this;
		}

		bool RenderTextureManagerS::IsActive()
		{
			return myIsActive;
		}
	}
}